<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>行情</title>
</head>
<style type="text/css">
    body {
        margin: 0;
        padding: 0;
        background-color: #1f212d
    }

    .Kline-div {
        width: 100%;
        bottom: 0rem;
        top: 0;
    }

    .Kline-div .m-line {
        height: 400px;
        width: 100%;
    }

    .Kline-div .K-line {
        height: 400px;
        width: 100%;
    }

</style>
<body>

<div id="Kline-div" class="Kline-div">


    <div style="margin-left: 10px;margin-right: 10px;color: white" class="stock">
        <table border="0" width="100%">
            <tr>
                <td><p class="zdf"><span title="最新价" class="name">最新价：</span> <span title="最新价" id="now"></span></p>
                </td>
                <td><p class="zdf"><span title="今日开盘价" class="name">今开：</span> <span title="今日开盘价" id="open"></span></p>
                </td>
                <td><p class="zdf"><span title="昨日收盘价" class="name">昨收：</span> <span title="昨日收盘价" id="close"></span>
                </p></td>
            </tr>
            <tr>
                <td><p class="zdf"><span title="今日最高价" class="name">今高：</span> <span title="今日最高价" id="high"></span></p>
                </td>
                <td><p class="zdf"><span title="今日最低价" class="name">今低：</span> <span title="今日最低价" id="low"></span></p>
                </td>
                <td><p class="zdf"><span title="成交量" class="name">成交量：</span> <span title="成交量" id="volume"></span></p>
                </td>
            </tr>
        </table>
    </div>

    <div class="fund">
        <img id="fund_img" style="width: 100%">

    </div>

    <div id="m-line" class="m-line stock" ></div>
    <div id="k-line" class="K-line stock" style="margin-bottom:50px"></div>
</div>
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/echarts.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/tmpData.js" type="text/javascript" charset="utf-8"></script>
<script src="js/k-line.js" type="text/javascript" charset="utf-8"></script>
<script>
    function getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }

    const mChart = echarts.init(document.getElementById('m-line'));
    const kChart = echarts.init(document.getElementById('k-line'));

    //数据格式
    function fmtdig(Data, Mat, F, Unit, AutoF) {
        var res = Data;
        if (Data != "" && Data != "--" && Data != "-") {
            var _temp = Math.abs(parseFloat(Data));
            var temp = parseFloat(Data);
            if (AutoF) {
                if (_temp > 1000000000000)//万亿
                {
                    Mat = 100000000;
                    Unit = "亿";
                    F = "0";
                } else if (_temp > 100000000000)//千亿
                {
                    Mat = 100000000;
                    Unit = "亿";
                    F = "0";
                } else if (_temp > 10000000000)//百亿
                {
                    Mat = 100000000;
                    Unit = "亿";
                    F = "1";
                } else if (_temp > 1000000000)//十亿
                {
                    Mat = 100000000;
                    Unit = "亿";
                    F = "2";
                } else if (_temp > 100000000)//亿
                {
                    Mat = 100000000;
                    Unit = "亿";
                    F = "2";
                } else if (_temp > 10000000)//千万
                {
                    Mat = 10000;
                    Unit = "万";
                    F = "0";
                } else if (_temp > 1000000)//百万
                {
                    Mat = 10000;
                    Unit = "万";
                    F = "1";
                } else if (_temp > 100000)//十万
                {
                    Mat = 10000;
                    Unit = "万";
                    F = "2";
                } else if (_temp > 10000) {
                    Mat = 10000;
                    Unit = "万";
                    F = "2";
                } else if (_temp > 1000) {
                    Mat = 1;
                    Unit = "";
                    F = "2";
                } else if (_temp > 100) {
                    Mat = 1;
                    Unit = "";
                    F = "2";
                } else if (_temp > 10) {
                    Mat = 1;
                    Unit = "";
                    F = "2";
                } else {
                    Mat = 1;
                    Unit = "";
                    F = "3";
                }
            }
            res = ForDight((temp / Mat), F);
        }
        return res + Unit;
    }

    function ForDight(Dight, How) {
        rDight = parseFloat(Dight).toFixed(How);
        if (rDight == "NaN") {
            rDight = "--";
        }
        return rDight;
    }

    Date.prototype.Format = function (fmt) { //author: meizz
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
    function stock(code,type) {


        const stockNowPriceUrl = "https://push2.eastmoney.com/api/qt/stock/trends2/get?secid=" + code + "&fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13&fields2=f51,f53,f56,f58&iscr=0&iscca=0&ndays=1";

        const url1 = 'http://push2.eastmoney.com/api/qt/stock/get?ut=fa5fd1943c7b386f172d6893dbfba10b&fltt=2&invt=2&fields=f43,f44,f45,f46,f47,f48,f60&secid=' + code;

        const kline = "https://push2his.eastmoney.com/api/qt/stock/kline/get?klt=101&fqt=1&lmt=80&iscca=1&" +
            "fields1=f1,f2,f3,f4,f5&fields2=f51,f52,f53,f54,f55,f56,f57,f59&ut=f057cbcbce2a86e2866ab8877db1d059&forcect=1&secid=" + code + "&end=" + (new Date()).Format("yyyyMMdd");
        // get(stockInfoUrl, function (res) {
        // 	console.log(res);
        // })
        //f60 昨收，f43 最新， f44 最高，f45 最低,f46,今开 f47：成交量，f48，成交额
        /**
         * {
         *     data:{},
         *
         * }
         */


		get(url1, function (res) {

            // document.getElementById("aaaa").innerHTML = "bushiba"
            /**
             * id="now"
             * id="open">4.64元<
             id="close">4.64元
             id="high">4.67元<
             id="low">4.63元</
             id="volume"
             id="amount"
             */
            const data = res.data;


            $("#now").html(data.f43)
            $("#open").html(data.f46)
            $("#close").html(data.f60)
            $("#high").html(data.f44)
            $("#low").html(data.f45)
            $("#volume").html(fmtdig(data.f47, 1, 2, "", true) + "手")


            $("#amount").html(data.f48)

        });
		get(stockNowPriceUrl, function (res) {
			mChart.setOption(initMOption({data: res.data.trends, yestclose: res.data.prePrice}, type));
		})

        get(kline, function (res) {
            kChart.setOption(initKOption(res.data.klines))
        })

    }

    function fund(code) {
        const url = "https://fundmobapi.eastmoney.com/FundMApi/FundVarietieValuationDetail.ashx?deviceid=Wap&plat=Wap&product=EFund&version=2.0.0&_=" + new Date().getTime() + "&FCODE=" + code
        get(url, function (res) {

        })
    }

    function get(url, success) {
        $.ajax({
                url: url,
                type: 'GET',
                beforeSend: function (XHR) {
                    console.log("开始请求")
                },
                success: function (res) {
                    success(res)
                },
                error: function (res) {
                    console.log("失败：" + res)
                }, complete: function (XHR, TS) {
                    console.log("完成：")
                }
            }, 'json'
        )
    }

</script>
<script>
    let code = getQueryString("code");
    let type = getQueryString("type");
    let isStock = code.indexOf(".");
    let $stock = $(".stock");
    let $fund = $(".fund")
    let $fund_img = $("#fund_img")
    if (isStock>0) {
        $stock.show()
        $fund.hide()
        stock(code,type);
    } else {
        $fund.show()
        $stock.hide()
        let imgurl = "http://j4.dfcfw.com/charts/pic6/" + code + ".png?v=" + new Date().getTime() + "?v=0.990607489607439";
        $fund_img.attr("src",imgurl)
    }


</script>
</body>
</html>
